package com.manager.kudian.system.web;

import com.manager.kudian.common.persistence.Page;
import com.manager.kudian.common.persistence.PropertyFilter;
import com.manager.kudian.common.utils.PropertiesLoader;
import com.manager.kudian.common.web.BaseController;
import com.manager.kudian.system.dao.OrderInfoDao;
import com.manager.kudian.system.entity.*;
import com.manager.kudian.system.service.*;
import com.manager.kudian.system.vo.OrderDeliveryVO;
import com.manager.kudian.system.vo.OrderInfoVO;
import com.manager.kudian.system.vo.SellerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色controller
 * @author ty
 * @date 2015年1月13日
 */
@Controller
@RequestMapping("business/order")
public class OrderController extends BaseController{
	
	@Autowired
	private OrderInfoDao orderInfoDao;

	@Autowired
	private OrderInfoService orderInfoService;

	@Autowired
	private MediaResService mediaResService;

	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "business/orderList";
	}

	/**
	 * 获取字典json
	 */
	@RequestMapping(value="json",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> orderList(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Page<OrderInfo> page = getPage(request);
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);

			PropertiesLoader p = new PropertiesLoader("category.properties");
			// 特产店铺主键
			String sellerid = p.getProperty("SPE_SHOP_ID");

			if (filters != null) {
				PropertyFilter filter = new PropertyFilter("EQI_sellerId", sellerid);
				filters.add(filter);
			}
			Page<OrderInfo> ps = orderInfoDao.findPage(page, filters);

			List<OrderInfoVO> l = new ArrayList<OrderInfoVO>();
			for (OrderInfo oi : ps.getResult()) {
				OrderInfoVO v = new OrderInfoVO();
				BeanUtils.copyProperties(oi, v);

				l.add(v);
			}

			map.put("rows", l);
			map.put("total", page.getTotalCount());
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping(value = "ordergoodsdetail/{id}", method = RequestMethod.GET)
	public String ordergoodsdetail(@PathVariable("id") Integer id, Model model) {

		OrderInfo oi = orderInfoService.get(id);
		model.addAttribute("oi", oi);
		return "business/ordergoods";
	}

	/**
	 * 获取订单中商品json
	 */
	@RequestMapping(value="goodsjson/{id}",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> ordergoodsList(@PathVariable("id") Integer id, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Page<OrderGoods> page = getPage(request);
			// List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
			page = this.orderInfoService.ordergoodsList(page, id);

			map.put("rows", page.getResult());
			map.put("total", page.getTotalCount());
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 获取订单中商品json
	 */
	@RequestMapping(value="allgoodsjson/{id}",method = RequestMethod.GET)
	@ResponseBody
	public List<OrderGoods> allordergoodsList(@PathVariable("id") Integer id) {
		List<OrderGoods> ret = new ArrayList<OrderGoods>();
		try {
			ret = this.orderInfoService.allordergoodsList(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 获取订单中运单json
	 */
	@RequestMapping(value="deliveryorderlistjson/{id}",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> deliveryorderlist(@PathVariable("id") Integer id, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Page<DeliveryOrder> page = getPage(request);
			// List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
			// page = this.orderInfoService.ordergoodsList(page, id);
			page = this.orderInfoService.deliveryorderlist(page, id);
			map.put("rows", page.getResult());
			map.put("total", page.getTotalCount());
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping(value = "createdelivery/{id}", method = RequestMethod.GET)
	public String createdeliveryForm(@PathVariable("id") Integer id, Model model) {

		model.addAttribute("orderId", id);
		return "business/orderdeliveryForm";
	}

	@RequestMapping(value = "adddeliveryForm", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> adddeliveryForm(@RequestBody OrderDeliveryVO vo) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			this.orderInfoService.adddeliveryForm(vo);
			map.put("errcode", 0);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("errcode", 9999);
		}

		return map;
	}

	@RequestMapping(value = "delete/{deliveryId}")
	@ResponseBody
	public String delete(@PathVariable("deliveryId") Integer deliveryId) {
		orderInfoService.delete(deliveryId);
		return "success";
	}

	@RequestMapping(value = "viewdelivery/{deliveryId}", method = RequestMethod.GET)
	public String viewdeliveryForm(@PathVariable("deliveryId") Integer deliveryId, Model model) {

		model.addAttribute("deliveryId", deliveryId);
		return "business/orderdeliveryView";
	}

	/**
	 * 获取运单中商品列表json
	 */
	@RequestMapping(value="deliverygoodslistjson/{deliveryId}",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> deliverygoodslist(@PathVariable("deliveryId") Integer deliveryId, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Page<DeliveryGoods> page = getPage(request);
			// List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
			// page = this.orderInfoService.ordergoodsList(page, id);
			page = this.orderInfoService.deliverygoodslist(page, deliveryId);
			map.put("rows", page.getResult());
			map.put("total", page.getTotalCount());
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
}
